﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using VNWIKI_WEB;
using VNWIKI_WEB.Models;

namespace VNWIKI_WEB.Controllers
{ 
    public class CategoryController : Controller
    {
        VNWIKIDataContext db = new VNWIKIDataContext();

        //
        // GET: /Category/

        public ActionResult Index()
        {
            User admin = (User)Session["user"];
            if (admin == null || admin.Admin == false)
                return RedirectToAction("LogOn", "Admin");

            return View(db.Categories.ToList());
        }

        //
        // GET: /Category/Details/5

        public ActionResult Details(int id)
        {
            User admin = (User)Session["user"];
            if (admin == null || admin.Admin == false)
                return RedirectToAction("LogOn", "Admin");

            Category category = db.Categories.Single(u => u.ID == id);
            return View(category);
        }

        //
        // GET: /Category/Create

        public ActionResult Create()
        {
            User admin = (User)Session["user"];
            if (admin == null || admin.Admin == false)
                return RedirectToAction("LogOn", "Admin");

            return View();
        } 

        //
        // POST: /Category/Create

        [HttpPost]
        public ActionResult Create(Category category)
        {
            if (ModelState.IsValid)
            {
                db.Categories.InsertOnSubmit(category);
                db.SubmitChanges();
                return RedirectToAction("Index");  
            }

            return View(category);
        }
        
        //
        // GET: /Category/Edit/5
 
        public ActionResult Edit(int id)
        {
            User admin = (User)Session["user"];
            if (admin == null || admin.Admin == false)
                return RedirectToAction("LogOn", "Admin");

            Category category = db.Categories.Single(u => u.ID == id);
            return View(category);
        }

        //
        // POST: /Category/Edit/5

        [HttpPost]
        public ActionResult Edit(Category category)
        {
            if (ModelState.IsValid)
            {
                Category cat = db.Categories.Single(u => u.ID == category.ID);
                cat.Name = category.Name;
                cat.CreateDate = category.CreateDate;
                cat.Deleted = category.Deleted;
                cat.DeleteDate = category.DeleteDate;
                cat.Albums = category.Albums;
                db.SubmitChanges();
                return RedirectToAction("Index");
            }
            return View(category);
        }

        //
        // GET: /Category/Delete/5
 
        public ActionResult Delete(int id)
        {
            User admin = (User)Session["user"];
            if (admin == null || admin.Admin == false)
                return RedirectToAction("LogOn", "Admin");

            Category category = db.Categories.Single(u => u.ID == id);
            return View(category);
        }

        //
        // POST: /Category/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            Category category = db.Categories.Single(u => u.ID == id);

            List<Album> albumList;
            albumList = (from c in db.Albums
                         where c.CategoryID.Equals(id)
                         select c).ToList();
            foreach (var al in albumList)
            {
                List<Photo> photoList;
                photoList = (from c in db.Photos
                             where c.AlbumID.Equals(al.ID)
                             select c).ToList();
                foreach (var p in photoList)
                {
                    db.Photos.DeleteOnSubmit(p);
                }

                db.Albums.DeleteOnSubmit(al);
            }

            db.Categories.DeleteOnSubmit(category);
            db.SubmitChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}